package com.xx.happychatboot.base.filter;

import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileSizeLimitInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        if (ex instanceof FileSizeLimitExceededException || ex instanceof MaxUploadSizeExceededException) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("文件上传大小超过限制");
            response.getWriter().flush();
        }
    }
}
